<template>
  <div class="app-container">
    <basic-table
      :table-title="tableTitle"
      :button-group="buttonGroup"
      :multiple-table="false"
      :search-form="searchForm"
      :data-source="dataSource"
      :data-params="dataParams"
      :reload="reload"
      @closeReload="closeReload"
    />
    <el-dialog
      v-if="dialogFormVisible"
      :title="operationType === 'add'? '注册IM用户' : '编辑IM用户'"
      :visible.sync="dialogFormVisible"
      top="3%"
      @close="cancel"
    >
      <div class="el-dialog-div">
        <data-form
          v-if="dialogFormVisible"
          :loading="loading"
          :edit-data="editData"
          :operation-type="operationType"
          @createDataEmit="createDataEmit"
          @dialogFormVisibleEmit="dialogFormVisibleEmit"
        />
      </div>
    </el-dialog>
    <el-dialog :title="'修改密码'" :visible.sync="dialogPasswordVisible" top="3%">
      <div class="el-dialog-div">
        <password-form
          :passwordtemp="passwordtemp"
          @createPassword="createPassword"
          @dialogPasswordVisibleEmit="dialogPasswordVisibleEmit"
        />
      </div>
    </el-dialog>
  </div>
</template>
<script>
import DataForm from './components/dataForm.vue'
import BasicTable from '@/components/BasicTable/index.vue'
import PasswordForm from '@/views/imUser/components/passwordForm.vue'

import { AddUser, GetUserPage, UpdateUser, ChangerUserPassWord } from '@/api/User'
import { TipsBox } from '@/utils/feedback.js'
import { jsonToParams, jsonToFormData } from '@/utils'

export default {
  name: 'ImUser',
  components: { BasicTable, DataForm, PasswordForm },
  data() {
    return {
      dialogFormVisible: false,
      dialogPasswordVisible: false,
      passwordtemp: {},
      dataSource: () => GetUserPage,
      dataParams: {},
      loading: false,
      reload: false,
      tableTitle: [
        {
          label: '用户名',
          value: 'UserName',
          show: true
        },
        {
          label: '性别',
          value: 'Gender',
          show: true,
          transfer: ['保密', '女', '男'],
          width: '60'
        },
        {
          label: '电话',
          value: 'Mobile',
          show: true
        },
        {
          label: '公司名称',
          value: 'CompanyName',
          show: true
        },
        {
          label: '部门名称',
          value: 'DepartmentName',
          show: true
        },
        {
          label: '职位',
          value: 'PositionName',
          show: true
        },
        {
          label: '添加好友',
          value: 'AllowType',
          show: true,
          transfer: ['允许', '需要同意', '不允许'],
          width: '100'
        },
        {
          label: '创建时间',
          value: 'CreateOn',
          show: true,
          width: '200'
        },
        {
          label: '操作',
          show: true,
          type: 'options',
          fixed: 'right',
          width: '290',
          options: [
            {
              text: '编辑',
              icon: 'el-icon-edit',
              type: 'success',
              callback: row => {
                this.dialogFormVisible = true
                this.operationType = 'edit'
                this.editData = row
              }
            },
            {
              text: '密码',
              icon: 'el-icon-key',
              type: 'primary',
              callback: row => {
                this.resetPasswordtemp()
                this.dialogPasswordVisible = true
                this.passwordtemp.userId = row.UserId
              }
            },
            {
              text: '聊天记录',
              icon: 'el-icon-chat-dot-square',
              type: 'info',
              callback: row => {
                this.$router.push({
                  name: 'ImUserRecords',
                  query: { userId: row.UserId, userName: row.UserName, routerAva:row.Avatar }
                })
              }
            }
          ]
        }
      ],
      editData: {},
      operationType: 'add',
      buttonGroup: {
        expend: true,
        show: true,
        title: '表格筛选',
        size: 'small',
        fields: [
          {
            showButtonGroup: true,
            text: '注册',
            icon: 'el-icon-check',
            callback: param => {
              this.editData = {}
              this.dialogFormVisible = true
              this.operationType = 'add'
            }
          }
        ]
      },
      searchForm: {
        expend: true,
        title: '表格筛选',
        size: 'small',
        fields: [
          {
            show: true,
            type: 'input',
            label: '用户名',
            labelShow: true,
            name: 'userName'
          },
          {
            show: true,
            type: 'input',
            label: '公司',
            labelShow: true,
            name: 'companyName'
          },
          {
            show: true,
            type: 'input',
            label: '电话',
            labelShow: true,
            name: 'mobile'
          }
        ]
      }
    }
  },
  methods: {
    closeReload(val) {
      this.reload = val
    },
    resetPasswordtemp() {
      this.passwordtemp = {
        passWord: '',
        userId: ''
      }
    },
    createPassword(v) {
      this.loading = true
      ChangerUserPassWord(jsonToParams(v)).then((res) => {
        this.loading = false
        if (res.statusCode === 200) {
          TipsBox('success', res.message)
          this.dialogPasswordVisible = false
        }
      })
    },
    dialogPasswordVisibleEmit(v) {
      this.dialogPasswordVisible = v
    },
    //
    cancel() {
      this.dialogFormVisible = false
    },
    dialogFormVisibleEmit(v) {
      this.dialogFormVisible = v
    },
    // add
    createDataEmit(v) {
      const formData = jsonToFormData(v)
      let operate = null
      if (this.operationType === 'add') {
        // 注册
        operate = AddUser
      } else if (this.operationType === 'edit') {
        // 编辑
        operate = UpdateUser
      }

      if (!operate) {
        return
      }

      this.loading = true
      operate(formData).then((res) => {
        this.loading = false
        let type = 'error'
        if (res.statusCode === 200) {
          this.dialogFormVisible = false
          type = 'success'
          this.reload = true
        }
        TipsBox(type, res.message)
      }).catch(err => {
        console.log('error', err)
        this.loading = false
      })
    }
  }
}
</script>
